<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="listQuery" size="small" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="listQuery.title" placeholder="请输入标题" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="getDataList()">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="handleResetSearch()">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="danger" @click="deleteHandle()"
            :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div class="table-container">
      <el-table :data="list" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="title" label="标题" header-align="center" align="center"></el-table-column>
        <el-table-column prop="brief" label="简介" header-align="center" align="center"></el-table-column>
        <el-table-column prop="picUrl" label="风采图片" header-align="center" align="center">
          <template slot-scope="{row}">
            <el-image
              :src="baseApi + row.picUrl"
              :preview-src-list="[baseApi + row.picUrl]"
              fit="contain"
              lazy
              class="el-avatar"
            >
              <div slot="error">
                <i class="el-icon-document" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
            <el-tag v-if="scope.row.status === 1" size="small">正常</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="note" label="备注" header-align="center" align="center"></el-table-column>
        <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button type="danger" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页控件 -->
    <el-pagination background class="table-pagination" @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle" :current-page="listQuery.pageIndex" :page-size="listQuery.pageSize"
      :total="total" :page-sizes="[10, 20, 50, 100]" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import { fetchCustomList, deleteCustom } from '@/api/custom/custom'
import AddOrUpdate from './custom-add-or-update'
const baseUrl = process.env.VUE_APP_FILE_URL === '/' ? '' : process.env.VUE_APP_FILE_URL

const defaultListQuery = {
  pageIndex: 1,
  pageSize: 10,
};

export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: [],
      total: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      baseApi: baseUrl,
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 重置查询表单
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      fetchCustomList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.listQuery.pageSize = val
      this.listQuery.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.listQuery.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true

      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })

    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[删除]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCustom(ids).then(res => {
          if (res && res.code === 200) {

            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1000
            })

            this.getDataList()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => { })
    },
  }
}
</script>
